<?php
namespace app\api\controller;
use app\api\model\HousingResources;
use app\api\model\ElecOrder;
use app\api\model\ElectricityMeter;
use app\api\model\Room;
use app\api\model\Tenant;
use app\api\model\Tenergy;
use think\Db;
use think\Request;

class Api extends Common
{
    public function check(Request $request)
    {
        $pwd = $request->passwd;
        $str = 'qwer123456';
        $res = [];
        $res['code'] = '200';
        $res['msg'] = 'ok';
        if($pwd != $str){
            $res['code'] = '500';
            $res['msg'] = 'error';
        }
        return json($res);
    }
    //获取房源列表
    public function homeList(Request $request)
    {
        $param = [];
        if(!empty($request->addr)){
            $param = explode(' ', $request->addr);
        }
        $list = HousingResources::getList($param);
        $res = [];
        $res['code'] = '200';
        $res['list'] = $list;
        if (!$list) {
            $res['code'] = '500';
        }
        return json($res);
    }

    //获取房间列表
    public function roomList(Request $request)
    {
        $tch_id = $request->tch_id;
        if(empty($tch_id)){
            $res['code'] = '500';
            $res['list'] = '';
            return json($res);
        }
        $list = Room::getRoomList($tch_id);
        $res = [];
        $res['code'] = '200';
        $res['list'] = $list;
        if (!$list) {
            $res['code'] = '500';
        }
        return json($res);
    }

    //绑定门锁
    public function bindLock(Request $request)
    {
        $r_id = $request->r_id;
        if(empty($r_id)){
            $ren['code'] = '500';
            return json($ren);
        }
        $data = [];
        $data['door_lock'] = $request->lock_num;
        $res = Room::bindData($r_id, $data);
        $ren = [];
        $ren['code'] = '200';
        if (!$res) {
            $ren['code'] = '500';
        }
        return json($ren);
    }

    //绑定电表
    public function bindElec(Request $request)
    {
        $r_id = $request->r_id;
        if(empty($r_id)){
            $ren['code'] = '500';
            return json($ren);
        }
        $data = [];
        $data['elec_meter'] = $request->elec_num;
        $data['update_time'] = time();
        $res = Room::bindData($r_id, $data);
        ////日志
        $logContent = "\r\n";
        $logContent .= "Datetime: " . date("Y-m-d H:i:s") . ' time: ' . time() . "\r\n";
        $logContent .= "Data: " . json_encode($data) . "\r\n";
        $logContent .= "Sql: " . Room::getLastSql() . "\r\n";
        $logContent .= "Res: " . $res . "\r\n";
        $this->writeLog($logContent, "bindElec");
        $ren = [];
        $ren['code'] = '200';
        if (!$res) {
            $ren['code'] = '500';
        }
        return json($ren);
    }

    //新增电表
    public function addElec(Request $request)
    {
        if ($request->deviceName) {
            if (isset($request->t)) {
                $roomInfo = Room::getId($request->deviceName);
                if ($roomInfo) {
                    $energy['r_id'] = $roomInfo['r_id'];
                    $energy['h_id'] = $roomInfo['house_id'];
                }
                $energy['TEnergy'] = $request->t;
                $energy['name'] = $request->deviceName;
                $energy['recording_time'] = date('Y-m-d', time());
                $energy['create_time'] = time();
                $res1 = Tenergy::addInfoOnly($energy);
                ////日志
                $logContent = "\r\n";
                $logContent .= "Datetime: " . date("Y-m-d H:i:s") . ' time: ' . time() . "\r\n";
                $logContent .= "Res: " . $res1 . "\r\n";
                $this->writeLog($logContent, "addEnergy");
                $data['name'] = $request->deviceName;
                $data['status'] = 1;
                $data['create_time'] = time();
            } else {
                $data['name'] = $request->deviceName;
                $data['clientIp'] = $request->clientIp;
                $data['status'] = 1;
                $data['create_time'] = time();
            }
            $res = ElectricityMeter::addElec($data);
            ////日志
            $logContent = "\r\n";
            $logContent .= "Datetime: " . date("Y-m-d H:i:s") . ' time: ' . time() . "\r\n";
            $logContent .= "Data: " . json_encode($data) . "\r\n";
            $logContent .= "Sql: " . ElectricityMeter::getLastSql() . "\r\n";
            $logContent .= "Res: " . $res . "\r\n";
            $this->writeLog($logContent, "addElec");
            if ($res) {
                $this->addElecDetail($request->deviceName);
                $response['code'] = '200';
            } else {
                $response['code'] = '500';
            }
            return json($response);
        } else {
            return 'No data received';
        }
    }

    //获取电表当前状态json
    public function addElecDetail($name)
    {
        $data = $this->getProperty($name);
        $ren = json_decode($data, true);
        $update = [];
        foreach ($ren['data'] as $k => $v) {
            if ($v['attribute'] == 'Status') {
                $update['elecStatus'] = $v['value'];
            }
            if ($v['attribute'] == 'speed') {
                $update['speed'] = $v['value'];
            }
        }
        $res = ElectricityMeter::updateInfo($name, $update);
        ////日志
        $logContent = "\r\n";
        $logContent .= "Datetime: " . date("Y-m-d H:i:s") . ' time: ' . time() . "\r\n";
        $logContent .= "Updata: " . json_encode($update) . "\r\n";
        $logContent .= "Sql: " . ElectricityMeter::getLastSql() . "\r\n";
        $logContent .= "Res: " . $res . "\r\n";
        $this->writeLog($logContent, "addElec");
        return $res;
    }

    //获取电表详情
    public function getProperty($name)
    {
        $url = 'http://49.235.160.151:8082/getProperty?device=' . $name;
        $data = $this->curlData($url, '', 'GET');
        return $data;
    }

    //根据电表获取房间详细地址
    public function getRoomAddr(Request $request)
    {
        if ($request->deviceName) {
            $name = $request->deviceName;
            $houseInfo = Room::getRoomInfo($name);
            if ($houseInfo) {
                $ren['code'] = '200';
                $ren['info'] = $houseInfo;
            } else {
                $ren['code'] = '500';
                $ren['info'] = '';
            }
            return json($ren);
        } else {
            return 'No data received';
        }
    }
}